import Global from "../../global";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MiniMap extends cc.Component {

    @property(cc.Node)
    MapCamera:cc.Node = null;     //战斗地图
    @property(cc.Camera)
    MiniCamera:cc.Camera = null;    //小地图摄像机
    @property(cc.Sprite)    
    ShowSprite:cc.Sprite = null;    //渲染到的地方
    @property(cc.Node)
    Area:cc.Node = null;            //当前正在展示的区域

    //偏移量
    deviation:cc.Vec2;

    onLoad(){
        //把地图渲染到图片上
        this.ShowSprite.node.scaleY = -1;
        const texture = new cc.RenderTexture();
        texture.initWithSize(400,400);
        const spriteFrame = new cc.SpriteFrame();
        spriteFrame.setTexture(texture);
        this.ShowSprite.spriteFrame = spriteFrame;
        this.MiniCamera.targetTexture = texture;
        //开启小地图滑动监听
        this.node.on(cc.Node.EventType.TOUCH_START,this.click,this);
        //记录偏移量
        this.deviation = this.node.convertToWorldSpaceAR(cc.Vec2.ZERO);
    }


    /**
     * 开启或者关闭小地图
     */
    openOrClose(){
        if(this.node.active){
            this.node.active = false;
        }else{
            this.node.active = true;
        }
    }

    /**
     * 按下
     */
    click(event:cc.Event.EventTouch){    
        this.Area.setPosition(event.getLocation().sub(this.deviation));
        if(this.Area.x<-130){
            this.Area.x=-130;
        }else if(this.Area.x>130){
            this.Area.x=130;
        }
        if(this.Area.y<-150){
            this.Area.y=-150;
        }else if(this.Area.y>150){
            this.Area.y=150;
        }
        cc.tween(this.MapCamera).to(.2,{position:cc.v3(this.Area.x*10+2000,this.Area.y*10+2000)}).start();
    }


    update(){
       this.Area.setPosition((this.MapCamera.x-2000)/10,(this.MapCamera.y-2000)/10);
    }
}
